package com.chatroomdemo.common.config;

import com.chatroomdemo.controller.TestWebSocketController;
import com.chatroomdemo.controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket//启动websocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestWebSocketController testWebSocketController;

    @Autowired
    private WebSocketController webSocketController;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketController,"/WebSocketMessage")
                //！！！由于userId 和 username 都是保存在httpSession中的，无法直接在webSocketSession中使用
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
